﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class IslandLogic : MonoBehaviour 
{

	float exp = 100;

	int healthState = 2;

	float lowerSpeed = 2;

	public int getHealthCount = 0;

	int level = 1;

	public Text text;

	public Image image;

	public Sprite[] spriteArray;

	public GameObject effect;

	public GameObject appearEffect;

	public Text scoreText;

	int scoreValue;

	// Use this for initialization
	void Start () 
	{
		effect.SetActive(false);
		appearEffect.SetActive(false);
		scoreText.gameObject.SetActive(false);
		scoreValue = 0;
	}
	
	// Update is called once per frame
	void Update () 
	{
		text.text = healthState.ToString();

		//exp -= lowerSpeed * Time.deltaTime;

		/*if(exp < 0)
		{
			gameObject.SetActive(false);
		}*/
	}
	public void CheckNeedLowHealth()
	{
		if(getHealthCount == 0 && healthState != 0)
		{
			// TODO:添加状态变化
			if(healthState > 0)
			{
				healthState --;
			}
			if(healthState >= 0)
			{
				image.sprite = spriteArray[healthState];
			}

			if(healthState == 0)
			{
				GetComponent<BoxCollider2D>().enabled = false;

				CreateItem.Instance.deadIslandCount++;
			}

		//image.sprite = 
		}
		else
		{
			getHealthCount = 0;
		}

		scoreValue = 0;
	}

	void UpdateIslandImageSprite()
	{
		if(healthState == 0)
		{
			image.sprite = spriteArray[0];
		}
		else
		{
			//Debug.Log("sprite index:" + ((level - 1) * 2 + healthState));
			image.sprite = spriteArray[(level - 1) * 2 + healthState];
		}

	}

	private void OnTriggerEnter2D(Collider2D other) 
	{
		exp += 10;
		if(exp > (level + 1) * 100 && level < 5)
		{
			level ++;

			UpdateIslandImageSprite();
		}

		var ballLevel = other.GetComponent<BallLevel>();

		LightAnimationManager.Instance.PlayAnimation();
		SoundManager.Instance.PlayGetSound();

		//Debug.Log("====== Ball Level:" + ballLevel.ballLevel);

		scoreValue += level * ballLevel.ballLevel;
		ScoreManager.Instance.AddScore((level * ballLevel.ballLevel));
		CreateItem.Instance.objCount --;
		other.gameObject.SetActive(false);
		CreateItem.Instance.GenerateMiddleBall(level);
		getHealthCount ++;
		
		if(healthState == 1) // 如果当前是枯萎状态，需要再回到之前健康的状态 
		{
			healthState = 2;

			// 更新状态显示
			// TODO:添加后续的新的sprite类型
			//image.sprite = spriteArray[2];
			UpdateIslandImageSprite();
		}

		Destroy(other.gameObject);

		StartCoroutine(ShowGetEffect());
		StartCoroutine(ShowGetScore());
		//Debug.Log("======= exp:" + exp);
		//Debug.Log("-------- OnTriggerEnter ---------");
	}

	IEnumerator ShowGetEffect()
	{
		effect.SetActive(true);
		appearEffect.SetActive(true);
		yield return new WaitForSeconds(1);
		effect.SetActive(false);
		appearEffect.SetActive(false);
	}

	IEnumerator ShowGetScore()
	{
		if(scoreText.gameObject.activeSelf)
		{
			yield break;
		}
		scoreText.gameObject.SetActive(true);
		float timeCount = 0;
		while(timeCount < 1)
		{
			timeCount += Time.deltaTime;
			scoreText.text = "+" + scoreValue;
			yield return null;
		}
		scoreText.gameObject.SetActive(false);
	}
}
